#!/bin/bash
#
# Copyright (c) 2017 MariaDB Corporation Ab
#
# Use of this software is governed by the Business Source License included
# in the LICENSE.TXT file.
#
# Change Date: 2021-12-01
#
# On the date above, in accordance with the Business Source License, use
# of this software will be governed by version 2 or later of the General
# Public License.
#
MCS_DIR="/usr/local/mariadb/columnstore"

# capture getSystemStatus and remove first 9 lines and blank lines to just have status table contents
STATUS=$($MCS_DIR/bin/mcsadmin getSystemStatus | tail -n +9  | sed '/^$/d' )
# grab system status line
SYSTEM_STATUS=$(echo "$STATUS" | grep 'System' | awk '{ printf $2; }')
# combine module status lines
MODULE_STATUS=$(echo "$STATUS" | grep 'Module' | awk '{ printf $2 ":" $3 " "; }')
# set module status to 0 as default
MODULE_STAT="0"

for i in $(echo $MODULE_STATUS | sed 's/Parent\:OAM//')
do
	if [[ "$i" =~ "ACTIVE" ]]
	then
		:
	else
		# set module status to 1 if an error occur
		MODULE_STAT=1
	fi
done

# if system status is ACTIVE, then all good otherwise consider critical failure
if [ "$SYSTEM_STATUS" == "ACTIVE" -a "$MODULE_STAT" -eq 0 ]
then
	echo "OK - system: $SYSTEM_STATUS, modules: $MODULE_STATUS"
  	exit 0
else
	echo "CRITICAL - system: $SYSTEM_STATUS, modules: $MODULE_STATUS"
	exit 2
fi
